package com.ayz.gateway.common.constants;


import java.io.Serializable;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName DataVersion
 * @Description TODO: 数据的版本号
 * @Author 快乐的星球
 * @Date 2023/5/13 16:31
 * @Version 1.0
 **/
public class DataVersion implements Serializable {
    /**
     * 时间戳
     */
    private long timestamp;

    /**
     * 数据的版本号
     */
    private final AtomicInteger dataVersion;

    /**
     * 下一个版本，迭代一个版本
     */
    public void nextVersion(){
        this.timestamp=System.currentTimeMillis();
        this.dataVersion.getAndIncrement();
    }

    public DataVersion() {
        this.timestamp = System.currentTimeMillis();
        this.dataVersion=new AtomicInteger(0);
    }

    public DataVersion(long timestamp, int dataVersion) {
        this.timestamp = timestamp;
        this.dataVersion = new AtomicInteger(dataVersion);
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public Integer getDataVersion() {
        return dataVersion.get();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DataVersion that = (DataVersion) o;
        return getTimestamp() == that.getTimestamp() && getDataVersion().equals(that.getDataVersion());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getTimestamp(), getDataVersion());
    }
}
